import {get} from '@/utils/request'

const state = {
  channels: [], // 渠道
  directionClazzTree:[],  // 班级树
  employees:[] ,// 讲师
  charges:[], // 班主任
  dormitoryTypes:[], // 宿舍类型
}

const mutations = {
  
  SET_DORMITORYTYPES: (state, dormitoryTypes) => {
    state.dormitoryTypes = dormitoryTypes
  },
  SET_CHANNELS: (state, channels) => {
    state.channels = channels
  },
  SET_DIRECTION_CLAZZ_TREE: (state, directionClazzTree) => {
    state.directionClazzTree = directionClazzTree
  },
  SET_EMPLOYEES: (state, employees) => {
    state.employees = employees
  },
}

const actions = {
  // 加载宿舍类型信息
  loadDormitoryTypes({commit}){
    return new Promise(async resolve => {
      let url = "/dormitoryType/findAll"
      get(url,).then(response => {
        commit('SET_DORMITORYTYPES',response.data)
        resolve();
      })
    })
  },
  // 加载讲师信息
  loadEmployees({ commit }) {
    return new Promise(async resolve => {
      let url = "/employee/pageQuery"
      get(url,{
        page:1,
        pageSize:100,
        deptId:1  //教学部
      }).then(response => {
        commit('SET_EMPLOYEES',response.data.list)
        resolve();
      })
    })
  },
  loadChannels({ commit }) {
    return new Promise(async resolve => {
      let url = "/channel/queryTree"
      get(url,{
        page:1,
        pageSize:100
      }).then(response => {
        // 去除空孩子节点
        foo(response.data.list)
        commit('SET_CHANNELS',response.data.list)
        resolve();
      })
    })
  },
  // 加载方向班级树
  loadDirectionClazzTree({ commit }) {
    return new Promise(async resolve => {
      let url = "/direction/queryDirectionClazzTree"
      get(url).then(response => {
        commit('SET_DIRECTION_CLAZZ_TREE',response.data)
        resolve();
      })
    })
  },

  loadDirections(){
    
  },
}

function foo(privileges) {
  for (const p of privileges) {
    if (p.children && p.children.length === 0) {
      delete p.children
    } else {
      foo(p.children)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
